package com.chaodev.base.controller;

import cn.hutool.core.bean.BeanUtil;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.GrowthSystem;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.GrowthSystemService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/growthSystem")
@Api(tags = "成长体系相关接口")
public class GrowthSystemController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private GrowthSystemService growthSystemService;

    /**
     * 获取成长体系配置信息列表
     */
    @ApiOperation(value = "获取成长体系配置信息列表", notes = "获取成长体系配置信息列表")
    @PostMapping(value = {"/list"})
    public Result list(@RequestBody GrowthSystem dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            GrowthSystem growthSystem = new GrowthSystem();
            BeanUtil.copyProperties(dto,growthSystem);
            List<GrowthSystem> growthSystemList = growthSystemService.queryList(growthSystem);

            result.setCode(200);
            result.setMsg("获取成功");
            result.setData(growthSystemList);
            logService.logAdd(null, "获取成长体系配置信息列表", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取成长体系配置信息列表", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "获取成长体系配置信息列表(无token)", notes = "获取成长体系配置信息列表")
    @PostMapping(value = {"/listOut"})
    public Result listOut(@RequestBody GrowthSystem dto, HttpServletRequest request) {
        Result result = new Result();
        GrowthSystem growthSystem = new GrowthSystem();
        BeanUtil.copyProperties(dto,growthSystem);
        List<GrowthSystem> growthSystemList = growthSystemService.queryList(growthSystem);
        result.setCode(200);
        result.setMsg("获取成功");
        result.setData(growthSystemList);
        logService.logAdd(null, "获取成长体系配置信息列表", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 获取成长体系配置信息表对象
     */
    @ApiOperation(value = "获取成长体系配置信息表对象", notes = "获取成长体系配置信息表对象")
    @GetMapping(value = {"/getDataById/{id}"})
    public Result findById(@PathVariable Integer id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            GrowthSystem dataById = growthSystemService.findDataById(id);

            result.setCode(200);
            result.setData(dataById);
            logService.logAdd(null, "获取成长体系配置信息表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取成长体系配置信息表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存成长体系信息对象
     */
    @ApiOperation(value = "保存成长体系信息对象", notes = "保存成长体系信息对象")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody GrowthSystem growthSystem,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;

        if (growthSystem.getAccumulatedDuration() ==null && growthSystem.getPoints()==null){
            if(growthSystem.getGradeName()==null){
                result.setCode(500);
                result.setMsg("参数为空");
                logService.logAdd(null, "获取成长体系配置信息列表", "ERROR", result.toString(), userInfo, request);
            }else {
                result.setCode(500);
                result.setMsg("缺少参数");
                logService.logAdd(null, "获取成长体系配置信息列表", "ERROR", result.toString(), userInfo, request);
            }
        }

        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            growthSystemService.saveData(growthSystem,userInfo);

            result.setCode(200);
            result.setMsg("操作成功");
            result.setData(growthSystem);
            logService.logAdd(null, "保存成长体系信息对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存成长体系信息对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除成长体系信息实体类对象
     */
    @ApiOperation(value = "删除成长体系信息表对象", notes = "删除成长体系信息表对象")
    @GetMapping(value = {"/del"})
    public Result delete(@RequestParam List<Integer> ids, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            growthSystemService.deleteDataById(ids);

            result.setCode(200);
            logService.logAdd(null, "删除成长体系信息表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除成长体系信息表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

}
